var article = document.getElementsByClassName('article')[0];
var articleTitle = article.getElementsByClassName('article_title')[0];
var articleContent = article.getElementsByClassName('article_content')[0];
var articleClassify = article.getElementsByClassName('article_classify')[0];
var articleTime = article.getElementsByClassName('article_time')[0];
var articleContinue = article.getElementsByClassName('article_continue')[0];

var footer = document.getElementsByClassName('footer')[0];
var footerList = footer.getElementsByClassName('footer_list')[0];
var footerPages = footer.getElementsByClassName('footer_pages')[0];

var commend = document.getElementsByClassName('commend')[0];
var commendList = commend.getElementsByClassName('commend_list')[0]

var hot = document.getElementsByClassName('hot')[0];
var hotList = hot.getElementsByClassName('hot_list')[0];

var headerLogin = document.getElementsByClassName('header_login')[0];
//登录按钮挂载监听器
headerLogin.addEventListener('click',function(){
    location.href='/admin'
})

//跳转页面函数
function skipPage(e) {
    e.addEventListener('click', function () {
        var id = e.getAttribute('id')
        var url = '/getPage?id=' + id
        location.href = url
    })
}
//填充最新的一篇文章
function renderArticle(e) {
    e = JSON.parse(e)[0]
    // console.log(e)
    articleTitle.innerHTML = e.title
    articleClassify.innerHTML = e.classify
    articleContinue.setAttribute('id', e._id)
    articleContent.innerHTML = e.content.slice(0, 100) + '……'
    articleTime.innerHTML = e.time
    skipPage(articleContinue)
}
//渲染五篇文章
function renderNewArticle(e) {
    e = JSON.parse(e)
    footerList.innerHTML = ""
    e.forEach((item) => {
        var li = document.createElement('li')
        li.setAttribute('id', item._id)
        li.className = 'footer_item'
        li.innerHTML = item.title
        footerList.appendChild(li)
        skipPage(li)
    })
}
//渲染点击次数最多的五篇文章
function renderHotArticleList(e) {
    // console.log(e)
    e = JSON.parse(e)
    // footerList.innerHTML=""
    e.forEach((item) => {
        var li = document.createElement('li')
        li.className = "hot_item"
        li.setAttribute('id', item._id)
        li.innerText = item.title
        hotList.appendChild(li)
        skipPage(li)
    })
}
//渲染最新的五条评论
function renderNewCommend(e) {
    e = JSON.parse(e)
    e.forEach((item) => {
        // console.log(item)
        var li = document.createElement('li')
        li.className = "commend_item"
        li.setAttribute('id', item.fromId)
        li.innerHTML = item.commend
        commendList.appendChild(li)
        skipPage(li)
    })
}
//渲染翻页按钮
function renderArticleNum(e) {
    e = Math.ceil(e / 5)
    for (var i = 1; i <= e; i++) {
        var li = document.createElement('li')
        li.className = 'pages'
        li.innerHTML = i
        addEvent(li)
        footerPages.appendChild(li)
    }
}
//给翻页按钮添加监听器
function addEvent(li) {
    li.onclick = function () {
        var num = li.innerText
        myAjax('get', '/getNewArticle?page=' + num, null).then((e) => {
            renderNewArticle(e)
        })
    }
}
async function getData() {
    var getArticle = await myAjax('get', '/getArticle', null)
    renderArticle(getArticle)
    var getHotArticleList = await myAjax('get', '/getHotArticleList', null)
    renderHotArticleList(getHotArticleList)
    var getNewCommendList = await myAjax('get', '/getNewCommendList')
    renderNewCommend(getNewCommendList)
    var getNewArticle = await myAjax('get', '/getNewArticle', null)
    renderNewArticle(getNewArticle)
    var getArticleNum = await myAjax('get', 'getArticleNum', null)
    renderArticleNum(getArticleNum)
}
getData()